package com.flex.mobile.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.IGenericDAO;
import com.flex.mobile.dao.model.EvaluateInfo;

@Service("evaluateInfoDao")
public class EvaluateInfoDao implements IGenericDAO<EvaluateInfo> {
	private final SimpleJdbcTemplate template;

	private final SimpleJdbcInsert insertContact;

	@Autowired
	public EvaluateInfoDao(DataSource dataSource) {
		this.template = new SimpleJdbcTemplate(dataSource);
		this.insertContact = new SimpleJdbcInsert(dataSource).withTableName("EVALUATE_INFO").usingGeneratedKeyColumns("ID");
	}

	private final RowMapper<EvaluateInfo> summaryRowMapper = new ParameterizedRowMapper<EvaluateInfo>() {
		@Override
		public EvaluateInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			EvaluateInfo item = new EvaluateInfo();
			item.setId(rs.getLong("ID"));
			item.setOrderId(rs.getLong("ORDER_ID"));
			item.setDriverId(rs.getInt("DRIVER_ID"));
			item.setOwnerId(rs.getLong("OWNER_ID"));
			item.setFinishDate(rs.getDate("FINISH_DATE"));
			item.setEvaAttitude(rs.getInt("EVA_ATTITUDE"));
			item.setEvaClean(rs.getInt("EVA_CLEAN"));
			item.setEvaPunctual(rs.getInt("EVA_PUNCTUAL"));
			return item;
		}
	};

	@Override
	public EvaluateInfo create(EvaluateInfo item) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ID", null);
		parameters.put("ORDER_ID", item.getOrderId());
		parameters.put("DRIVER_ID", item.getDriverId());
		parameters.put("OWNER_ID", item.getOwnerId());
		parameters.put("FINISH_DATE", item.getFinishDate());
		parameters.put("EVA_ATTITUDE", item.getEvaAttitude());
		parameters.put("EVA_CLEAN", item.getEvaClean());
		parameters.put("EVA_PUNCTUAL", item.getEvaPunctual());
		Number id = this.insertContact.executeAndReturnKey(parameters);
		item.setId(id.longValue());
		return item;
	}

	@Override
	public List<EvaluateInfo> findAll() {
		return this.template.query("SELECT * FROM EVALUATE_INFO ", this.summaryRowMapper);
	}

	@Override
	public EvaluateInfo findById(long id) {
		String sql = "select * from EVALUATE_INFO e" + " where e.id = ? ";
		return this.template.queryForObject(sql, summaryRowMapper, id);
	}

	@Override
	public List<EvaluateInfo> findByName(String name) {
//		String sql = "select * from EVALUATE_INFO e " + " where e.NAME  like ?";
//		return this.template.query(sql, summaryRowMapper, "%"+name.trim()+"%");
		return null;
	}

	@Override
	public boolean remove(EvaluateInfo item) {
		int count = this.template.update("DELETE FROM EVALUATE_INFO WHERE id=?", item.getId());
		return count == 1;
	}

	@Override
	public boolean update(EvaluateInfo item) {
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE EVALUATE_INFO SET ORDER_ID=?, DRIVER_ID=?, OWNER_ID=?,FINISH_DATE=?, EVA_ATTITUDE=?,");
		sql.append("EVA_CLEAN=?,EVA_PUNCTUAL=? ");
		sql.append("WHERE id=?");
		int count = this.template.update(sql.toString(), item.getOrderId(), item.getDriverId(), item.getOwnerId(), item.getFinishDate(), item.getEvaAttitude(), item.getEvaClean(), item
				.getEvaPunctual(), item.getId());
		return count == 1;
	}

}
